package com.ruoyi.zhmk.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.faceApi.entity.FaceIdentification;
import com.ruoyi.zhmk.domain.ZhmkDoorDevice;
import com.ruoyi.zhmk.domain.ZhmkPerson;
import com.ruoyi.zhmk.mapper.ZhmkDoorDeviceMapper;
import com.ruoyi.zhmk.mapper.ZhmkPersonMapper;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.zhmk.mapper.ZhmkAskMapper;
import com.ruoyi.zhmk.domain.ZhmkAsk;
import com.ruoyi.zhmk.service.IZhmkAskService;

/**
 * 人员请假Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-06-24
 */
@Service
public class ZhmkAskServiceImpl implements IZhmkAskService 
{
    @Autowired
    private ZhmkAskMapper zhmkAskMapper;
    @Autowired
    private ZhmkPersonMapper zhmkPersonMapper;
    @Autowired
    private ZhmkDoorDeviceMapper zhmkDoorDeviceMapper;

    /**
     * 查询人员请假
     * 
     * @param id 人员请假ID
     * @return 人员请假
     */
    @Override
    public ZhmkAsk selectZhmkAskById(Long id)
    {
        return zhmkAskMapper.selectZhmkAskById(id);
    }

    /**
     * 查询人员请假列表
     * 
     * @param zhmkAsk 人员请假
     * @return 人员请假
     */
    @Override
    public List<ZhmkAsk> selectZhmkAskList(ZhmkAsk zhmkAsk)
    {
        return zhmkAskMapper.selectZhmkAskList(zhmkAsk);
    }

    /**
     * 新增人员请假
     * 
     * @param zhmkAsk 人员请假
     * @return 结果
     */
    @Override
    public int insertZhmkAsk(ZhmkAsk zhmkAsk)
    {
        //判断人员是否可以请假（判断该次请假开始时间是否在未结束订单记录内，并且要大于当前时间，记录内，是否）
        //查询出最近的请假记录，判断该记录是否已完成

        //请假开始时间，必须大于当前时间
        Date currentDate = new Date();
        Date askStartDate = zhmkAsk.getAskStartDate();
        if(currentDate.getTime()>askStartDate.getTime()){
            //不能进行请假
            throw new CustomException("请假开始时间不能小于当前时间", HttpStatus.ERROR);
        }
        //查询出是否有未清算完成的重复请假记录
        ZhmkAsk zhmkAsk1 = zhmkAskMapper.selectZhmkAskNotAccomplish(zhmkAsk);
        if(zhmkAsk1 != null){
            throw new CustomException("该人员请假时间内存在未结束的请假记录",HttpStatus.ERROR);
        }
        //查询出未完成的请假记录
        ZhmkAsk zhmkAsk2 = zhmkAskMapper.selectZhmkAskNotAcc(zhmkAsk);
        if(zhmkAsk2 != null){
            throw new CustomException("该人员存在未结束的请假记录",HttpStatus.ERROR);
        }
        List<SysRole> roles = SecurityUtils.getLoginUser().getUser().getRoles();
        for(SysRole role : roles){  //一级审核员添加one
            if(role.getRoleKey().equals("one")){
                zhmkAsk.setRoleCode("one");  //二级审核员添加two
            }else if(role.getRoleKey().equals("two")){
                zhmkAsk.setRoleCode("two");
            }else{//三级审核员添加three
                zhmkAsk.setRoleCode("three");
            }
        }
        zhmkAsk.setDeptId(SecurityUtils.getLoginUser().getUser().getDeptId());
        zhmkAsk.setCreateTime(DateUtils.getNowDate());
        return zhmkAskMapper.insertZhmkAsk(zhmkAsk);
    }

    /**
     * 修改人员请假
     * 
     * @param zhmkAsk 人员请假
     * @return 结果
     */
    @Override
    public int updateZhmkAsk(ZhmkAsk zhmkAsk)
    {
        //判断修改状态，为已归队，添加归队时间为当前时间
        Integer userType = zhmkAsk.getUserType();
        if(userType.intValue() == 2){
            //添加归队时间为当前时间
            zhmkAsk.setInTime(new Date());
            //逾期归来改为归队的同时将人员管理请假人员改为在位人员
            ZhmkPerson person = new ZhmkPerson();
            person.setId(zhmkAsk.getUserId());
            person.setUserStatus(0);
            zhmkPersonMapper.updateZhmkPerson(person);
        }
        zhmkAsk.setUpdateTime(DateUtils.getNowDate());
        return zhmkAskMapper.updateZhmkAsk(zhmkAsk);
    }

    /**
     * 批量删除人员请假
     * 
     * @param ids 需要删除的人员请假ID
     * @return 结果
     */
    @Override
    public int deleteZhmkAskByIds(Long[] ids)
    {
        return zhmkAskMapper.deleteZhmkAskByIds(ids);
    }

    /**
     * 删除人员请假信息
     * 
     * @param id 人员请假ID
     * @return 结果
     */
    @Override
    public int deleteZhmkAskById(Long id)
    {
        return zhmkAskMapper.deleteZhmkAskById(id);
    }

    /**
     * 判断是否为请假人员，是修改请假记录信息
     * @param faceIdentification
     * @return
     */
    @Override
    public int askOpinion(FaceIdentification faceIdentification, Map<String,Object> map) {
        //根据人员id获取人员
        String personId = faceIdentification.getPersonId();
        ZhmkPerson zhmkPerson = zhmkPersonMapper.selectZhmkPersonById(Long.parseLong(personId));
        if(zhmkPerson == null){
            return 0;
        }
        Integer userStatus = zhmkPerson.getUserStatus();
        if(userStatus == 1){
            //请假人员
            //根据设备id查询出是进门还是出门
            String deviceID = faceIdentification.getDeviceID();
            ZhmkDoorDevice zhmkDoorDevice = zhmkDoorDeviceMapper.selectZhmkDoorDeviceByDeviceId(deviceID);
            Integer inoutType = zhmkDoorDevice.getInoutType();
            if(inoutType == 0){
                //进
                //修改请假信息
                ZhmkAsk zhmkAsk = new ZhmkAsk();
                zhmkAsk.setUserId(Long.parseLong(personId));
                ZhmkAsk zhmkAsk1 = zhmkAskMapper.selectZhmkAskRecently(zhmkAsk);
                if(zhmkAsk1 != null){
                    zhmkAsk1.setInTime(DateUtils.getNowDate());
                    //已归队
                    zhmkAsk1.setUserType(2);
                    zhmkAskMapper.updateZhmkAsk(zhmkAsk1);
                }
            }else{
                //出
                //查询出人员最近的请假记录
                ZhmkAsk zhmkAsk = new ZhmkAsk();
                zhmkAsk.setUserId(Long.parseLong(personId));
                ZhmkAsk zhmkAsk1 = zhmkAskMapper.selectZhmkAskRecently(zhmkAsk);
                if(zhmkAsk1 != null){
                    //已离队
                    zhmkAsk1.setUserType(0);
                    //离队时间为空时，插入离队时间
                    if(zhmkAsk1.getOutTime() == null){
                        zhmkAsk1.setOutTime(DateUtils.getNowDate());
                    }
                    zhmkAskMapper.updateZhmkAsk(zhmkAsk1);

                }
                //请假事由
                map.put("reason",zhmkAsk1.getReasons());
                //审核人
                map.put("auditor",zhmkAsk1.getAuditor());

            }

        }


        return 0;
    }

    /**
     * 审核人员请假
     * @param zhmkAsk
     * @return
     */
    @Override
    public int check(ZhmkAsk zhmkAsk) {
        zhmkAsk.setUpdateTime(DateUtils.getNowDate());
        if(zhmkAsk.getStatus()==1){//移交中
            if(zhmkAsk.getRoleCode().equals("one")){//一级审核员移交two
                zhmkAsk.setRoleCode("two");
            }else if(zhmkAsk.getRoleCode().equals("two")){//一级审核员移交three
                zhmkAsk.setRoleCode("three");
            }
        }
        if(zhmkAsk.getStatus()==2 || zhmkAsk.getStatus()==3){ //通过或不通过存入当前审批用户的名字
            zhmkAsk.setAuditor(SecurityUtils.getLoginUser().getUser().getUserName());
        }
        return zhmkAskMapper.updateZhmkAsk(zhmkAsk);
    }

    /**
     * 查询人员请假待办事项列表
     *
     * @param zhmkAsk 人员请假
     * @return 人员请假集合
     */
    @Override
    public List<ZhmkAsk> selectZhmkAskToDoList(ZhmkAsk zhmkAsk){
        List<SysRole> roles = SecurityUtils.getLoginUser().getUser().getRoles();
        for(SysRole role : roles){  //一级审核员添加one
            if(role.getRoleKey().equals("one")){
                zhmkAsk.setRoleCode("one");  //二级审核员添加two
            }else if(role.getRoleKey().equals("two")){
                zhmkAsk.setRoleCode("two");
            }else{//三级审核员添加three
                zhmkAsk.setRoleCode("three");
            }
        }
        return zhmkAskMapper.selectZhmkAskToDoList(zhmkAsk);
    }
}
